﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Robot.CrawlerService.Request.QuanLaoDa;
using Robot.CrawlerService.Response.QuanLaoDa;
using Robot.CrawlerService.Crawler;
using System.Text.RegularExpressions;

namespace Robot.CrawlerService.Worker.QuanLaoDa
{
    public class PromotionListWorker:ICrawlerWorker<PromotionListRequest, PromotionListResponse>
    {
        static Regex regex = new Regex(@"<h3><a href=""/cuxiaodetail/(\d*).html"" target=""_blank"">.*?</a></h3>");

        public PromotionListResponse Work(PromotionListRequest request)
        {
            string url = string.Format("http://www.quanlaoda.com/cuxiao.php?page={0}", request.PageNumber);
            HttpCrawler crawler = new HttpCrawler();
            string html = crawler.Grab(url);
            MatchCollection matches = regex.Matches(html);
            PromotionListResponse response = new PromotionListResponse();
            foreach (Match match in matches)
            {
                response.DetailIdList.Add(int.Parse(match.Groups[1].Value));
            }
            return response;
        }
    }
}
